package com.ruoyi.system.service.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.constant.CommonConstant;
import com.ruoyi.common.core.domain.entity.customer.CustomerIntentionDB;
import com.ruoyi.common.exception.global.CustomException;
import com.ruoyi.common.utils.check.ValidateUtils;
import com.ruoyi.system.mapper.customer.CustomerIntentionMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class IntentionServiceImpl {

    @Resource
    private CustomerIntentionMapper customerIntentionMapper;

    public void add(CustomerIntentionDB req) {
        if (ObjectUtils.isEmpty(req.getUserPhoneNumber())){
            throw new  CustomException("手机号不能为空");
        }
        // 校验手机号
        boolean isMobile = ValidateUtils.isMobile(req.getUserPhoneNumber());
        if (!isMobile){
            throw new CustomException("请填写正确的手机号");
        }


        if (ObjectUtils.isEmpty(req.getUserName())){
            throw new CustomException("称呼不能为空");
        }
        if (req.getUserName().length() > 90){
            throw new CustomException("称呼太长");
        }

        if (ObjectUtils.isEmpty(req.getGender())){
            throw new CustomException("性别不能为空");
        }
        List<Integer> genderList = Arrays.asList(0, 1, 2);
        if (!genderList.contains(req.getGender())){
            throw new CustomException("非法的性别值");
        }
        if (ObjectUtils.isNotEmpty(req.getRemark()) && req.getRemark().length() > 900){
            throw new CustomException("备注不能超过900字符");
        }
        // 校验不可以重复提交
        LambdaQueryWrapper<CustomerIntentionDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerIntentionDB::getUserPhoneNumber, req.getUserPhoneNumber());
        queryWrapper.eq(CustomerIntentionDB::getIntentionAddressNo, req.getIntentionAddressNo());
        queryWrapper.eq(CustomerIntentionDB::getIsActive, CommonConstant.CONSTANT_NUM.ONE);
        Long l = customerIntentionMapper.selectCount(queryWrapper);
        if (l > 0){
            throw new CustomException("请勿重复提交");
        }

        req.setId(null);
        customerIntentionMapper.insert(req);
    }

    public List<Map<String, Object>> findRemaining() {
        return null;
    }

    public List<CustomerIntentionDB> find(String userPhoneNumber) {
        if (ObjectUtils.isEmpty(userPhoneNumber)){
            throw new CustomException("请填写正确的手机号");
        }
        LambdaQueryWrapper<CustomerIntentionDB> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerIntentionDB::getUserPhoneNumber, userPhoneNumber);
        wrapper.eq(CustomerIntentionDB::getIsActive, CommonConstant.CONSTANT_NUM.ONE);
        wrapper.orderByDesc(CustomerIntentionDB::getId);
        return customerIntentionMapper.selectList(wrapper);
    }

    public void delete(Long[] ids) {
        if (ObjectUtils.isEmpty(ids)){
            throw new CustomException("请选择要删除的数据");
        }
        LambdaUpdateWrapper<CustomerIntentionDB> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(CustomerIntentionDB::getIsActive, CommonConstant.CONSTANT_NUM.ZERO);
        wrapper.in(CustomerIntentionDB::getId, Arrays.asList(ids));
        customerIntentionMapper.update(wrapper);
    }
}
